public class Solution {
    public int singleNumber(int[] nums) {
        int ones = 0, twos = 0;
        for (int num : nums) {
            // `twos` stores the bits that have appeared 2 times, `ones` stores the bits that have appeared 1 time
            twos |= ones & num;  // twos should accumulate the bits that are appearing twice
            ones ^= num;  // ones should accumulate the bits that have appeared once

            // If a bit appears 3 times, it should be cleared from both `ones` and `twos`
            int threes = ones & twos;
            ones &= ~threes;
            twos &= ~threes;
        }
        return ones;  // the result will be in `ones` since `ones` will store the bits that appear only once
    }
}
